package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jfinal.weixin.sdk.msg.in.InTextMsg;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import io.renren.entity.TradermEntity;
import io.renren.entity.UnionidmEntity;
import io.renren.entity.WxuserEntity;
import io.renren.service.TradermService;
import io.renren.service.UnionidmService;
import io.renren.service.WxuserService;
import net.dreamlu.weixin.annotation.WxMsgController;
import net.dreamlu.weixin.spring.DreamMsgControllerAdapter;
import org.springframework.beans.factory.annotation.Autowired;

@WxMsgController("/weixin/wx")
public class WeiXinMsgController extends DreamMsgControllerAdapter {

    @Autowired
    private UnionidmService unionidmService;
    @Autowired
    private WxuserService wxuserService;
    @Autowired
    private TradermService tradermService;

    @Override
    protected void processInFollowEvent(InFollowEvent inFollowEvent) {
        System.out.println("_______processInFollowEvent_______" + JSONObject.toJSONString(inFollowEvent));
        OutTextMsg defaultMsg = new OutTextMsg(inFollowEvent);
        // 关注
        if (InFollowEvent.EVENT_INFOLLOW_SUBSCRIBE.equals(inFollowEvent.getEvent())) {
            // 可将关注用户录入db，此处可以获取到用户openid
            String openId = inFollowEvent.getFromUserName();
            // 获取到  公众号的 openid 反解获取 unionid
            // 1️⃣ 若查询库里有则更新对于的 unionid 和 wxOpenid 。
            // 2️⃣ 若没有则新增一条数据
            //            OutTextMsg otm = new OutTextMsg(inFollowEvent);
            //            otm.setContent("感谢关注");
            //            render(otm);
            // 1 插入 记录
            UnionidmEntity unionidmEntity = unionidmService.findOneByOpenId(openId, "1");
            if (unionidmEntity == null) {
                unionidmEntity = unionidmService.saveInfo(openId);
            }
            String miniOpenId = "";
            // 根据 公众号的 UnionId  查询
            WxuserEntity wxuserEntity = wxuserService.findOneByUnionid(unionidmEntity.getUnionId());
            if (wxuserEntity != null) {
                wxuserEntity.setWxopenid(unionidmEntity.getOpenId());
                wxuserService.updateWxOpenIdByUnionId(wxuserEntity);
                miniOpenId = wxuserEntity.getMiniopenid();
            }
            // 根据 公众号的openid 查询
            WxuserEntity wxuserEntity2 = wxuserService.findOneByWxOpenid(unionidmEntity.getOpenId());
            if (wxuserEntity2 != null) {
                wxuserEntity2.setUnionid(unionidmEntity.getUnionId());
                wxuserService.updateUnionIdByWxOpenId(wxuserEntity2);
                miniOpenId = wxuserEntity.getMiniopenid();
            }
            if (StringUtils.isNotEmpty(miniOpenId)) {
                tradermService.updateWxOpenIdByMiniOpenId(unionidmEntity.getOpenId(), miniOpenId);
            }
            // 更新 TraderM表 的WXOpenID
            wxuserService.updateTradermFk(wxuserEntity);

            System.out.println("感谢关注");
        }
        // 取消关注
        if (InFollowEvent.EVENT_INFOLLOW_UNSUBSCRIBE.equals(inFollowEvent.getEvent())) {
            System.out.println("用户取消关注了");
            // 可将关注用户录入db，此处可以获取到用户openid
            String openId = inFollowEvent.getFromUserName();
            // 根据 公众号的 UnionId  查询
            WxuserEntity wxuserEntity = wxuserService.findOneByWxOpenid(openId);
            if (wxuserEntity != null) {
                wxuserService.setWxOpenIdIsNull(wxuserEntity.getWxuseroid());
            }
            TradermEntity tradermEntity = tradermService.findByWxOpenId(openId);

            if (tradermEntity != null) {
                tradermService.setWxOpenIdIsNull(tradermEntity.getTradermoid());
            }
        }
    }

    @Override
    protected void processInTextMsg(InTextMsg inTextMsg) {
    }

    @Override
    protected void processInMenuEvent(InMenuEvent inMenuEvent) {
    }
}